home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / STATLINE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-07  |  13.9 KB  |  644 lines

  1. #include "config.h"
  2. #include "ctype.h"
  3. #include <time.h>
  4. #include "dirutil.h"
  5. #include "session.h"
  6. #include "commands.h"
  7. #include "x.h"
  8. #ifdef MSDOS
  9. #include <conio.h>
  10. #endif
  11.  
  12. #if !defined(_lint)
  13. static char rcsid[] OPTIONAL = "$Id: statline.c,v 1.8 1997/09/07 21:18:28 root Exp root $";
  14. #endif
  15.  
  16.  
  17. int STATLINE = 0;
  18. static int statlines = 2;
  19. #define STATMODE_BBS        1
  20. #define STATMODE_CONF        2
  21. #define STATMODE_FTP        4
  22. #define STATMODE_TUT        8
  23. #define STATMODE_NNTP        16
  24. #define STATMODE_HTTP        32
  25. #define STATMODE_UPTIME        64
  26. #define STATMODE_SHORT        128
  27. #define STATMODE_MARQUEE    256
  28. static int statmode = STATMODE_FTP
  29. #ifdef MAILBOX
  30.     | STATMODE_BBS
  31. #endif
  32. #ifdef CONVERS
  33.     | STATMODE_CONF
  34. #endif
  35. #ifdef TUTOR
  36.     | STATMODE_TUT
  37. #endif
  38. #ifdef BROWSER
  39.     | STATMODE_MARQUEE
  40. #endif
  41.     ;
  42.  
  43. #ifdef BROWSER
  44. static int newsMarqueePosition = 0;
  45. static char *newsMarquee = NULLCHAR;
  46. #endif
  47.  
  48.  
  49. char *NextCmdMsg;
  50. short NextCmd;
  51. unsigned char SCREENwidth = 80, SCREENlength = 25;
  52. extern char statversion[], statversion_flow[];
  53. extern int BbsUsers, FtpUsers, Tutored, NntpUsers, HttpUsers;
  54. extern int main_exit;        /* from main program (flag) */
  55. extern uint32 Ip_addr;
  56. extern int Mprunning;
  57.  
  58.  
  59. #ifdef HOLDMONITOR
  60. extern int HOLDindicator;
  61. #endif
  62.  
  63. #ifdef ALERTMONITOR
  64. extern int ALERTindicator;
  65. #endif
  66.  
  67.  
  68. int BLOCKStatline = 0;
  69. int INStatline = 0;
  70. static int StatlineBeep = 1;
  71. struct session *ScreenOwner;    /* Session currently displayed */
  72.  
  73.  
  74. void statlog (const char *buf);
  75. void displayStatLine (int offset, int phase, int onlymarquee);
  76. void statLineToggle (int clearmarquee);
  77. static void my_strtime (char *str);
  78. static void getuptime (char *timebf);
  79. extern int CountConfUsers (void);
  80. #ifndef MSDOS
  81. extern int socklen (int s, int rtx);
  82. #endif
  83.  
  84.  
  85.  
  86. void
  87. statlog (const char *buf)
  88. {
  89.     NextCmd = 0;
  90.     free (NextCmdMsg);
  91.     NextCmdMsg = NULLCHAR;
  92.     if (Command && Command->screen->statline) {    /* Command status line is on */
  93.         NextCmdMsg = strdup (buf);
  94.         rip (NextCmdMsg);
  95.         NextCmd = 120;
  96.     }
  97. #ifdef XSERVER
  98.     xnotify (X_MSG);
  99. #endif
  100. }
  101.  
  102.  
  103.  
  104. static void
  105. my_strtime (char *str)
  106. {
  107. char *cp;
  108. time_t tm;
  109.  
  110.     (void) time (&tm);
  111.     cp = ctime (&tm);
  112.     strncpy (str, cp, 11);
  113.     strncpy (&str[11], &cp[10], 6);
  114.     str[17] = 0;
  115. }
  116.  
  117.  
  118.  
  119. void
  120. screendaemon (int i OPTIONAL, void *v1 OPTIONAL, void *v2 OPTIONAL)
  121. {
  122. int k;
  123.  
  124.     server_disconnect_io ();
  125.     for (;;) {
  126.         for (k = 0; k < 5; k++)    {
  127.             kpause (100L);
  128.             if (!k
  129. #ifdef BROWSER
  130.                 || statmode & STATMODE_MARQUEE
  131. #endif
  132.                 )
  133.                 displayStatLine (0, 1, (k == 0) ? 0 : 1);
  134.         }
  135.  
  136.         for (k = 0; k < 5; k++)    {
  137.             kpause (100L);
  138.             if (!k
  139. #ifdef BROWSER
  140.                 || statmode & STATMODE_MARQUEE
  141. #endif
  142.                 )
  143.                 displayStatLine (0, 0, (k == 0) ? 0 : 1);
  144.         }
  145.  
  146. #ifdef SCREENSAVER
  147.         screensaver ();
  148. #endif
  149.     }
  150. }
  151.  
  152.  
  153.  
  154. static void
  155. getuptime (char *timebf)
  156. {
  157. char *cp;
  158. int fill;
  159.  
  160.     cp = tformat (secclock ());
  161.     fill = 14 - (int) strlen (cp);
  162.     sprintf (timebf, "Up:%*.*s%s", fill, fill, " ", cp);
  163. }
  164.  
  165.  
  166.  
  167. void 
  168. displayStatLine (int offset, int phase, int onlymarquee)
  169. {
  170. struct session *sp;
  171. int stop, r;
  172. int iscmd = 0;
  173. char timebf[20];
  174. short beepit = 0;
  175. static short lastbeep = 0;
  176. int origx, origy, k;
  177. char mybuf[256], smallbuf[16];
  178. int do_prompt = 0;
  179. #ifdef MSDOS
  180. struct text_info tr;
  181. int savedattr, attr;
  182. struct session *tochk;
  183. #endif
  184.  
  185.  
  186.     /* if offset == 1 then we are turning it off, -1 is turning it on,
  187.        and 0 is leaving it the same and just updating it */
  188.        
  189.     /* if we should ignore this update due to a blocking event, return */
  190.     if (main_exit)
  191.         return;
  192.  
  193.     /* if this is at beginning of TNOS's life (and not all init'ed), exit */
  194.     if (Mprunning < 0)
  195.         return;
  196.  
  197.     if (BLOCKStatline)
  198.         return;
  199.  
  200.     /* if we have nothing to do, return */
  201.     if (ScreenOwner && !ScreenOwner->screen->statline && offset != 1)
  202.         return;
  203.  
  204.     /* if we do not own the screen, then return */
  205.     if (ScreenOwner != Current)
  206.         return;
  207.  
  208. #ifdef SCREENSAVER
  209.     /* if the screen saver is on, return */
  210.     if (ssenabled ())
  211.         return;
  212. #endif
  213.  
  214. #ifdef MSDOS
  215.     gettextinfo(&tr);
  216.     savedattr = tr.attribute;
  217.     tochk = Current;
  218.     if (Current == Command)
  219.         tochk = Lastcurr;
  220. #endif
  221.     origx = wherex ();        /* get current x and y positions */
  222.     origy = wherey ();
  223.  
  224.     /* mark if this is the Command Session's statline or not */
  225.     if (ScreenOwner == Command)
  226.         iscmd = 1;
  227.  
  228.     if (!iscmd && onlymarquee)
  229.         return;
  230.  
  231.     INStatline = 1;            /* mark statline as being updated */
  232.  
  233.     window (1, 1, SCREENwidth, (iscmd) ? (statlines + 1) : 2);
  234.  
  235. #ifdef MSDOS
  236.     attr = ((tr.attribute & 0x0f) << 4) + ((tr.attribute & 0x70) >> 4);
  237.     textattr ((offset == 1) ? tr.attribute : attr);
  238.     textattr ((offset == 1) ? tr.attribute : attr);
  239. #else
  240.     highvideo ();
  241. #endif
  242.     if (!onlymarquee)
  243.         clreol ();
  244.     if (offset != 1) {    /* if update required */
  245.         if (onlymarquee)
  246.             goto marqueeonly;
  247.         sprintf (mybuf, (ScreenOwner->flowmode) ? statversion_flow : statversion, Nsessions);
  248.         cputs (mybuf);
  249.         stop = 10;
  250.         if (Nsessions < (unsigned) stop)
  251.             stop = (int) Nsessions;
  252.         for (sp = Sessions; sp < &Sessions[stop + 1]; sp++) {
  253.             if (sp->type == FREE || sp->type == COMMAND || sp->type == TRACESESSION) {
  254. #ifdef MSDOS
  255.                 textattr (attr);
  256. #endif
  257.                 cputs ("   ");
  258.                 continue;
  259.             }
  260.             r = socklen (sp->output, 1);
  261. #ifdef MSDOS
  262.             textattr ( (r) ? attr | 0x80 : attr);
  263. #endif
  264.             if (r && !beepit && sp != ScreenOwner)
  265.                 beepit = 1;
  266. #ifdef MSDOS
  267.             if (tochk == sp)
  268.                 highvideo ();
  269. #endif
  270.             sprintf (mybuf, "%2d%c", sp->index, (r) ? '*' : ' ');
  271.             cputs (mybuf);
  272. #ifdef MSDOS
  273.             lowvideo ();
  274. #endif
  275.         }
  276.         clreol ();
  277.         k = (59 - ((stop + 1) * 3)) - (int) strlen (statversion);
  278.         if (SCREENwidth > 80)
  279.             k += (SCREENwidth - 80);
  280.         for ( ; k; k--)
  281.             cputs (" ");
  282. #ifdef HOLDMONITOR
  283.         if (HOLDindicator)
  284.             cputs ("!");
  285.         else
  286. #endif
  287.             cputs (" ");
  288. #ifdef ALERTMONITOR
  289.         if (ALERTindicator)
  290.             cputs ("@");
  291.         else
  292. #endif
  293.             cputs (" ");
  294.  
  295.         cputs ("  ");
  296.         if (statlines == 2 && statmode & STATMODE_UPTIME)
  297.             getuptime (timebf);
  298.         else    {
  299.             my_strtime (timebf);
  300.             if (!phase)
  301.                 timebf[14] = ' ';
  302.         }
  303. #ifdef MSDOS
  304.         textattr (attr);
  305. #endif
  306.         cputs (timebf);
  307.         cputs (" ");
  308.         if (iscmd) {
  309.             clreol ();
  310.             strcpy (mybuf, " ");
  311. #ifdef MAILBOX
  312.             if (statmode & STATMODE_BBS)    {
  313.                 sprintf (smallbuf, "%s=%2d ", (statmode & STATMODE_SHORT) ? "B" : "BBS", BbsUsers);
  314.                 strcat (mybuf, smallbuf);
  315.             }
  316. #endif
  317. #ifdef CONVERS
  318.             if (statmode & STATMODE_CONF)    {
  319.                 sprintf (smallbuf, "%s=%3d ", (statmode & STATMODE_SHORT) ? "C" : "CONF", CountConfUsers ());
  320.                 strcat (mybuf, smallbuf);
  321.             }
  322. #endif
  323.             if (statmode & STATMODE_FTP)    {
  324.                 sprintf (smallbuf, "%s=%2d ", (statmode & STATMODE_SHORT) ? "F" : "FTP", FtpUsers);
  325.                 strcat (mybuf, smallbuf);
  326.             }
  327. #ifdef TUTOR
  328.             if (statmode & STATMODE_TUT)    {
  329.                 sprintf (smallbuf, "%s=%2d ", (statmode & STATMODE_SHORT) ? "T" : "TUT", Tutored);
  330.                 strcat (mybuf, smallbuf);
  331.             }
  332. #endif
  333. #ifdef NNTPS
  334.             if (statmode & STATMODE_NNTP)    {
  335.                 sprintf (smallbuf, "%s=%2d ", (statmode & STATMODE_SHORT) ? "N" : "NNTP", NntpUsers);
  336.                 strcat (mybuf, smallbuf);
  337.             }
  338. #endif
  339. #ifdef HTTP
  340.             if (statmode & STATMODE_HTTP)    {
  341.                 sprintf (smallbuf, "%s=%2d ", (statmode & STATMODE_SHORT) ? "H" : "HTTP", HttpUsers);
  342.                 strcat (mybuf, smallbuf);
  343.             }
  344. #endif
  345.             cputs (mybuf);
  346.             k = 80 - ((int) strlen(mybuf) + 3);
  347.             if (SCREENwidth > 80)
  348.                 k += (SCREENwidth - 80);
  349.             if (statlines == 2)
  350.                 sprintf (mybuf, "- %-*.*s ", k, k, (NextCmd) ? NextCmdMsg : "");
  351.             else    {
  352.                 if (statmode & STATMODE_UPTIME)
  353.                     getuptime (timebf);
  354.                 else
  355.                     sprintf (timebf, " ");
  356.                 sprintf (mybuf, "%*.*s ", k + 2, k + 2, timebf);
  357.                 cputs (mybuf);
  358.                 k = 74;
  359.                 if (SCREENwidth > 80)
  360.                     k += (SCREENwidth - 80);        
  361.                 sprintf (mybuf, "     %-*.*s ", k, k, (NextCmd) ? NextCmdMsg : "");
  362.             }
  363.             cputs (mybuf);
  364.         }
  365.         if (NextCmd)
  366.             if (!--NextCmd) {
  367.                 free (NextCmdMsg);
  368.                 NextCmdMsg = 0;
  369. #ifdef XSERVER
  370.                 xnotify (X_MSG);
  371. #endif
  372.             }
  373.         if (!lastbeep && beepit && StatlineBeep)
  374.             cputs ("\007");
  375.         lastbeep = beepit;
  376. marqueeonly:
  377. #ifdef BROWSER
  378.         if (iscmd && (statmode & STATMODE_MARQUEE))    {
  379.             gotoxy (1, statlines);
  380.             marquee_display (newsMarquee, &newsMarqueePosition);
  381.         }
  382. #endif
  383.         window (1, 2 + ((iscmd) ? statlines - 1 : 0), SCREENwidth, SCREENlength - (ScreenOwner->split * 2));
  384.  
  385.         /* the last section replaces the cursor to the proper position
  386.            and depends on the value of 'offset'. If -1, then positioning
  387.            is adjusted, otherwise, it goes back to where it was. */
  388.         if (offset == -1 && iscmd)    {
  389.             offset = (-1 * statlines);
  390.             if (origy < statlines)    {
  391.                 origy = statlines + 1;
  392.                 origx = 1;
  393.                 do_prompt = 1;
  394.             }
  395.         }
  396.         gotoxy (origx, (int) (origy + offset));
  397.     } else {    /* otherwise, we are turning the statline off! */
  398.         if (iscmd) {
  399.             for (r = statlines - 1; r; r--)    {
  400.                 cputs ("\n");
  401.                 clreol ();
  402.             }
  403.         }
  404.         window (1, 1, SCREENwidth, SCREENlength - (ScreenOwner->split * 2));
  405.         gotoxy (origx, (int) (origy + 1 + ((iscmd) ? statlines - 1 : 0)));
  406.     }
  407. #ifdef MSDOS
  408.     textattr (savedattr);
  409. #else
  410.     normvideo ();
  411.     rflush ();
  412. #endif
  413.     INStatline = 0;
  414.     if (do_prompt)
  415.         netPrompt();
  416. }
  417.  
  418.  
  419.  
  420. int
  421. getStatlines (void)
  422. {
  423.     return statlines;
  424. }
  425.  
  426.  
  427.  
  428. /* Set status line on/off */
  429. int
  430. dostatline (int argc, char *argv[], void *p OPTIONAL)
  431. {
  432. int retval, itwas;
  433.  
  434.     itwas = STATLINE;
  435.     retval = setbool (&STATLINE, "Status line", argc, argv);
  436.     StatlineBeep = ((argc > 2) && (tolower (argv[2][0]) == 'q')) ? 0 : 1;
  437. #ifdef BROWSER
  438.     newsMarqueePosition = 0;
  439. #endif
  440.     if (STATLINE && !itwas) {
  441.         Command->screen->statline = uchar (STATLINE);
  442.         displayStatLine (-1, 1, 0);
  443.     }
  444.     if (!STATLINE && itwas) {
  445.         Command->screen->statline = uchar (STATLINE);
  446.         displayStatLine (1, 1, 0);
  447.     }
  448.     return (retval);
  449. }
  450.  
  451.  
  452.  
  453. /* Set status line mode */
  454. int
  455. dostatmode (int argc, char *argv[], void *p OPTIONAL)
  456. {
  457. int statwas;
  458.  
  459.     if (argc == 1)    {
  460.         /* Display active mode items */
  461.         tprintf ("The Command Session status line is configured with a %d line display:\n\t(using %s labels)\n",
  462.             statlines, (statmode & STATMODE_SHORT) ? "short" : "normal");
  463. #define prtstat(f, z)    tprintf ("%-21.21s: %s displayed\n", z, (statmode & f) ? "   " : "NOT");
  464.         prtstat(STATMODE_UPTIME, "System Uptime");
  465. #ifdef MAILBOX
  466.         prtstat(STATMODE_BBS, "BBS Users");
  467. #endif
  468. #ifdef CONVERS
  469.         prtstat(STATMODE_CONF, "Convers Users");
  470. #endif
  471.         prtstat(STATMODE_FTP, "FTP Users");
  472. #ifdef TUTOR
  473.         prtstat(STATMODE_TUT, "Tutor/News/Info Users");
  474. #endif
  475. #ifdef NNTPS
  476.         prtstat(STATMODE_NNTP, "NNTP Users");
  477. #endif
  478. #ifdef HTTP
  479.         prtstat(STATMODE_HTTP, "HTTP Users");
  480. #endif
  481. #ifdef BROWSER
  482.         prtstat(STATMODE_MARQUEE, "News Marquee");
  483. #endif
  484.         return 0;
  485.     }
  486.     /* Set active statline items */
  487.     (void) strupr (argv[1]);
  488.     statmode = 0;
  489.  
  490. #define onestat(c,f)    if (strchr (argv[1], c))    statmode |= f
  491. #ifdef MAILBOX
  492.     onestat('B', STATMODE_BBS);
  493. #endif
  494. #ifdef CONVERS
  495.     onestat('C', STATMODE_CONF);
  496. #endif
  497.     onestat('F', STATMODE_FTP);
  498. #ifdef TUTOR
  499.     onestat('T', STATMODE_TUT);
  500. #endif
  501. #ifdef NNTPS
  502.     onestat('N', STATMODE_NNTP);
  503. #endif
  504. #ifdef HTTP
  505.     onestat('H', STATMODE_HTTP);
  506. #endif
  507. #ifdef BROWSER
  508.     onestat('M', STATMODE_MARQUEE);
  509. #endif
  510.     onestat('U', STATMODE_UPTIME);
  511.     onestat('-', STATMODE_SHORT);
  512.     statwas = STATLINE;
  513.     if (statwas)
  514.         displayStatLine (1, 1, 0);
  515.     statlines = (strchr (argv[1], '3')) ? 3 : 2;
  516. #ifdef BROWSER
  517.     if (statmode & STATMODE_MARQUEE)    {
  518.         statlines++;
  519.         newsMarqueePosition = 0;
  520.     }
  521. #endif
  522.     if (statwas)
  523.         displayStatLine (-1, 1, 0);
  524.     return 0;    
  525. }
  526.  
  527.  
  528.  
  529. /* Status line toggle */
  530. void
  531. statLineToggle (int clearmarquee OPTIONAL)
  532. {
  533.     ScreenOwner->screen->statline ^= 1;
  534.     if (ScreenOwner == Command)
  535.         STATLINE = ScreenOwner->screen->statline;
  536.     displayStatLine ((ScreenOwner->screen->statline) ? -1 : 1, 1, 0);
  537. #ifdef BROWSER
  538.     if (clearmarquee)
  539.         newsMarqueePosition = 0;
  540. #endif
  541. }
  542.  
  543.  
  544.  
  545. void
  546. marquee_display (char *str, int *position)
  547. {
  548. char *buf;
  549. int len;
  550.  
  551. #if 0
  552.     if (str == NULLCHAR || !*str)
  553.         return;
  554. #endif
  555.  
  556.     buf = (char *) mallocw (SCREENwidth + 1);
  557.     if (!str || !*str || *position > (int) strlen (str))
  558.         *position = 0;
  559.     memset (buf, ' ', SCREENwidth);
  560.     if (*position < SCREENwidth)    {
  561.         if (*position)    {
  562.             len = SCREENwidth - *position;
  563.             sprintf (&buf[len], "%*.*s", *position,
  564.                 *position, (str) ? str : "");
  565.         }
  566.     } else
  567.         sprintf (buf, "%*.*s", SCREENwidth, SCREENwidth,
  568.             &str[*position - SCREENwidth]);        /*lint !e613 */
  569.  
  570.     buf[SCREENwidth] = 0;
  571.     (*position)++;
  572.  
  573.     cputs (buf);
  574.     free (buf);    
  575. }
  576.  
  577.  
  578. #ifdef BROWSER
  579.  
  580.  
  581. static int StatnewsEnabled = 0;
  582.  
  583. /* Start up statusline news service */
  584. int
  585. statnews1 (int argc OPTIONAL, char *argv[] OPTIONAL, void *p OPTIONAL)
  586. {
  587. char *cp;
  588. time_t now, realnow, then;
  589. int blockstate, part;
  590.  
  591.     if (StatnewsEnabled)    {
  592.         tprintf ("Statusline News Service already started: reloading URL\n");
  593.         ksignal (&newsMarquee, 1);
  594.         return 0;
  595.     }
  596.  
  597.     StatnewsEnabled = 1;
  598.     server_disconnect_io ();
  599.     log (-1, "Statusline News Service: started");
  600.     while (StatnewsEnabled)    {
  601.         cp = obtain_statline_marquee ();
  602.         if (cp != NULLCHAR)    {
  603.             blockstate = BLOCKStatline;
  604.             BLOCKStatline = 1;
  605.             while (INStatline)
  606.                 kwait (NULL);
  607.             free (newsMarquee);
  608.             newsMarquee = cp;
  609.             BLOCKStatline = blockstate;
  610.         }
  611.         realnow = time ((time_t *)0);
  612.         now = (realnow % 3600);
  613.  
  614.         /* 'now' contains seconds till minute zero - BUT....
  615.            if all TNOS sites are hitting the server at the
  616.            same second, this is BOUND to back up ones on slower
  617.            links, so we randomize this a bit, by adding the
  618.            last actet of the IP address (mod 60) to the count */
  619.  
  620.         part = (int) (Ip_addr & 0x00ff);
  621.         now -= (part % 60);
  622.         then = (3600 - now);
  623.         kalarm ((long)then * 1000L);
  624.         kwait (&newsMarquee);
  625.         kalarm (0);
  626.     }
  627.  
  628.     log (-1, "Statusline News Service: stopped");
  629.     return 0;
  630. }
  631.  
  632.  
  633.  
  634. /* Stop statusline news service */
  635. int
  636. statnews0 (int argc OPTIONAL, char *argv[] OPTIONAL, void *p OPTIONAL)
  637. {
  638.     StatnewsEnabled = 0;
  639.     ksignal (&newsMarquee, 1);
  640.     return 0;
  641. }
  642.  
  643. #endif
  644.